from PySide6.QtWidgets import (QMainWindow, QStatusBar, QToolBar, QMenu, 
                             QMessageBox, QFileDialog, QDockWidget)
from PySide6.QtCore import Qt, QSettings
from PySide6.QtGui import QAction, QIcon
from ui.project_tree import ProjectTreeDock
from ui.horizontal_visualization import HorizontalVisualizationWidget
from ui.vertical_visualization import VerticalVisualizationDock
from ui.table_window_manager import TableWindowManager
from core.data_model import DataModel
from core.view_controller import ViewController

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("三维地质勘探分析系统")
        print("初始化完成")
        self.resize(1400, 900)
        
        # 初始化模型和控制器
        self.data_model = DataModel()
        self.view_controller = ViewController()
        
        # 初始化UI
        self._init_ui()
        self._init_menu_toolbar()
        self._init_docks()
        self._init_central_area()
        self._init_status_bar()
        
        # 加载上次的窗口状态
        self._load_window_state()

    def _init_ui(self):
        """初始化基本UI设置"""
        self.setWindowTitle("三维地质勘探分析系统")
        self.resize(1400, 900)

    def _init_menu_toolbar(self):
        """初始化菜单栏和工具栏"""
        # 文件菜单
        file_menu = self.menuBar().addMenu("文件")
        new_action = QAction("新建", self)
        open_action = QAction("打开", self)
        save_action = QAction("保存", self)
        export_action = QAction("导出", self)
        file_menu.addActions([new_action, open_action, save_action, export_action])
        
        # 编辑菜单
        edit_menu = self.menuBar().addMenu("编辑")
        undo_action = QAction("撤销", self)
        redo_action = QAction("重做", self)
        edit_menu.addActions([undo_action, redo_action])
        
        # 视图菜单
        view_menu = self.menuBar().addMenu("视图")
        self.toggle_tree_action = QAction("显示项目树", self)
        self.toggle_property_action = QAction("显示属性面板", self)
        view_menu.addActions([self.toggle_tree_action, self.toggle_property_action])
        
        # 工具菜单
        tool_menu = self.menuBar().addMenu("工具")
        settings_action = QAction("设置", self)
        tool_menu.addAction(settings_action)
        
        # 窗口菜单
        window_menu = self.menuBar().addMenu("窗口")
        self.tile_action = QAction("平铺", self)
        self.cascade_action = QAction("层叠", self)
        window_menu.addActions([self.tile_action, self.cascade_action])
        
        # 帮助菜单
        help_menu = self.menuBar().addMenu("帮助")
        about_action = QAction("关于", self)
        help_menu.addAction(about_action)
        
        # 工具栏
        toolbar = QToolBar("主工具栏")
        toolbar.addActions([new_action, open_action, save_action, export_action])
        self.addToolBar(toolbar)

    def _init_docks(self):
        """初始化停靠窗口"""
        # 左侧项目树
        self.project_tree = ProjectTreeDock(self)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.project_tree)
        
        # 右侧属性面板
        self.property_dock = VerticalVisualizationDock(self)
        self.addDockWidget(Qt.RightDockWidgetArea, self.property_dock)
        
        # 连接信号
        self.toggle_tree_action.triggered.connect(self.project_tree.toggleViewAction().trigger)
        self.toggle_property_action.triggered.connect(self.property_dock.toggleViewAction().trigger)

    def _init_central_area(self):
        """初始化中央区域"""
        self.visualization = HorizontalVisualizationWidget(self)
        self.setCentralWidget(self.visualization)
        
        # 创建表格窗口管理器
        self.table_window = TableWindowManager(self)
        self.project_tree.open_table_requested.connect(self.table_window.open_table)

    def _init_status_bar(self):
        """初始化状态栏"""
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("就绪")

    def _load_window_state(self):
        """加载上次的窗口状态"""
        settings = QSettings()
        geometry = settings.value("geometry")
        if geometry:
            self.restoreGeometry(geometry)
        state = settings.value("windowState")
        if state:
            self.restoreState(state)

    def closeEvent(self, event):
        """窗口关闭事件"""
        settings = QSettings()
        settings.setValue("geometry", self.saveGeometry())
        settings.setValue("windowState", self.saveState())
        super().closeEvent(event)

    def update_visualization(self, filename, data):
        """更新可视化视图的数据"""
        self.visualization.update_data(filename, data)
        self.property_dock.update_data(filename, data) 